﻿/*************************************************************************
 *  Copyright © 2023-2030 Administrator. All rights reserved.
 *------------------------------------------------------------------------
 *  公司：DefaultCompany
 *  项目：CustomBuildProcess
 *  文件：BuildProcessOverride.cs
 *  作者：Administrator
 *  日期：2024/9/28 22:10:41
 *  功能：Nothing
*************************************************************************/

using System;
using UnityEditor;
using UnityEditor.Build;
using UnityEditor.Build.Reporting;

namespace CustomBuildProcess.Editor
{
    public static class BuildProcess
    {
        public static BuildPlayerOptions buildPlayerOptions;
        public static Action<BuildReport> OnPreprocessBuildCall;
        public static Action<BuildReport> OnPostprocessBuildCall;
    }

    public class BuildProcessOverride : IPreprocessBuildWithReport, IPostprocessBuildWithReport
    {
        public Action<BuildReport> OnPreprocessBuildCall;

        public Action<BuildReport> OnPostprocessBuildCall;

        [InitializeOnLoadMethod]
        static void RegisterBuildPlayerHandler()
        {
            // 注册自定义的构建方法
            BuildPlayerWindow.RegisterBuildPlayerHandler(CustomBuildHandler);
        }

        // 自定义构建处理方法
        private static void CustomBuildHandler(BuildPlayerOptions options)
        {
            BuildProcess.buildPlayerOptions = options;
            // 在构建之前弹出自定义的窗口
            CustomBuildSettingsWindow.ShowWindow();
        }

        // 在打包前执行的回调
        public int callbackOrder { get { return 0; } }

        public void OnPreprocessBuild(BuildReport report)
        {
            //Debug.Log("PreprocessBuild: 开始打包前的预处理...");
            // 在这里执行你想要在打包前进行的操作
            BuildProcess.OnPreprocessBuildCall?.Invoke(report);
        }

        // 在打包后执行的回调
        public void OnPostprocessBuild(BuildReport report)
        {
            //Debug.Log("PostprocessBuild: 打包完成后的后处理...");
            // 在这里执行你想要在打包后进行的操作
            BuildProcess.OnPostprocessBuildCall?.Invoke(report);
        }
    }
}